package gob.cgr.web.sgrh.managed;

import java.util.HashMap;
import java.util.Map;

import gob.cgr.sgrh.ejb.entidad.Rol;
import gob.cgr.sgrh.ejb.entidad.Usuario;
import gob.cgr.web.sgrh.service.SigerhService;
import gob.cgr.web.sgrh.util.DocxManipulator;
import gob.cgr.web.sgrh.util.Utilitario;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;

@SessionScoped
@ManagedBean
public class UsuarioMB {

@Inject
SigerhService service;
	
private Usuario	user;
private String userName;
private String password;
private boolean analistaRH;
private boolean colaborador;
private String sexo;

public UsuarioMB() {
	// TODO Auto-generated constructor stub
}

public String ingresar() {
	System.out.println("validando al usuario");
	Usuario user1=new Usuario();
	user1.setNombreUsuario(userName);
	user1.setPassword(password);
	user=service.validarUsuario(user1);
	
	if(user==null){
		Utilitario.mostrarMensajeAlerta("Las credenciales son invalidas, verifique su usuario y password");
		return "";
	}else{
		sexo=user.getIdPersona().getSexo();
		return "principal";
	}
	
	
	
}

public void reset(){
	userName="";
	password="";
}

public Usuario getUser() {
	return user;
}

public void setUser(Usuario user) {
	this.user = user;
}

public String getUserName() {
	return userName;
}

public void setUserName(String userName) {
	this.userName = userName;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

public boolean isAnalistaRH() {
	analistaRH=checkRol("ANALISTA RH");
	return analistaRH;
}

public void setAnalistaRH(boolean analistaRH) {
	this.analistaRH = analistaRH;
}

public boolean isColaborador() {
	colaborador=checkRol("COLABORADOR");
	
	
	return colaborador;
}

public void setColaborador(boolean colaborador) {
	this.colaborador = colaborador;
}

private boolean checkRol(String rol){
	if(user.getRolList()!=null ){
		for(Rol r: user.getRolList()){
			if(rol.toUpperCase().equals(r.getNombreRol().toUpperCase()))
				return true;
		}
	}
	return false;
}
public String logout(){
	user=null;
	reset();
	 FacesContext facesContext = FacesContext.getCurrentInstance();
	 HttpSession httpSession = (HttpSession)facesContext.getExternalContext().getSession(false);
	 httpSession.invalidate();
	return "principal";
}

public String getSexo() {
	return sexo;
}

public void setSexo(String sexo) {
	this.sexo = sexo;
}
public void crearDoc(){
	Map<String, String> m=new HashMap<String, String>();
	m.put("personal.nombre", "Atilio");
	DocxManipulator.generateAndSendDocx("template.docx", m);
	
}

}
